﻿//根据浏览器的hostname前缀，判断使用不同的url地址
//var ajaxurl = "http://api.ezezic.com.cn/api/GateWay";
var ajaxurl = "http://123.206.216.171:8080/api/GateWay";
var YiyiTongConfig = {
	merno: "10000",
	Url: ajaxurl,
	Datat: "json",
	AjaxType: "post",
	token: ""
};

function Base64Encrypt(str) {
	return window.btoa(unescape(encodeURIComponent(str)));
}

function Base64Decrypt(str) {
	return decodeURIComponent(escape(window.atob(str)));
}

YiyiTongConfig.Token = getAppToken();

 console.log(getAppToken())
// YiyiTongConfig.Token = 'token.18.h5.d2114cb569f92fc924ad699663245e1c';
function jAjax(messagetype, callback, bodyData) {
	var timestamp = Math.round(new Date().getTime() / 1000);
	var GloblData = {
		merno: YiyiTongConfig.merno,
		timestamp: timestamp,
		token: YiyiTongConfig.Token,
		service: messagetype,
		channel: "h5",
		version: "v1",
		data: Base64.encode(JSON.stringify(bodyData)),
		machineCode: "h5",
		sign: timestamp
	};
	$.ajax({
		type: YiyiTongConfig.AjaxType,
		async: false,
		url: YiyiTongConfig.Url,
		contentType: 'application/json;charset=utf-8',
		data: JSON.stringify(GloblData),
		success: callback,
		error: function (e) {
			debugger;
			alert(e.message)
		}
	});
}

function jsonpCallback(res) {
	alert(JSON.stringify(res))
}


function GetCurrentDevice() {

	return ("undefined" != typeof App) ? App : window;
}


function getAppToken() {
	try {
		return GetCurrentDevice().getToken();
	} catch (err) {
		return  Math.round(new Date().getTime() / 1000);
	}
}
//获取APP版本
function getAppVersion() {
	try{
		return GetCurrentDevice().getAppVersion();
	}catch(errr)
	{
		return "";
	}
}

//获取订单号
function getAppOrderNumber() {
	try{
		return GetCurrentDevice().getOrderNumber();
	}catch(errr)
	{
		return "";
	}
}
function showAppUploadPayImgUI(strOrderNumber) {
	try{
		GetCurrentDevice().showUploadPayImgUI(strOrderNumber);
	}catch(errr)
	{
		
	}
}

//设置标题
function setAppTitle() {
	try{
		GetCurrentDevice().setTitle(document.title);
	}catch(errr)
	{
		return "";
	}
}

//获取购物信息方法
function getAppGoodsForContract() {
	try {
		return GetCurrentDevice().getGoodsForContract();
	} catch (err) {
		return  "";
	}
}

//关闭窗口
function closeView() {
	try {
		return GetCurrentDevice().closeWebView();
	}catch(errr)
	{
	}
}


//截取小数点后两位
function fixedNumber(num) {
	var bb = num + "";
	var dian = bb.indexOf('.');
	var result = "";
	if (dian == -1) {
		result = num + ".00";
	} else {
		var cc = bb.substring(dian + 1, bb.length);
		if (cc.length >= 2) {
			result = bb.substring(0, bb.indexOf(".") + 3);
		} else {
			result = num + "0";
		}
	}
	return result;
	/*if(num<0){
		return (Math.floor(num* 100) / 100).toFixed(2);
    }else{
    	return (Math.ceil(num * 100) / 100).toFixed(2);
    }*/
}
//获取当前时间，使其展现形式与后台保持一致
function timeCompare() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var startTime = date.getFullYear() + seperator1 + month + seperator1 + strDate +
		" " + date.getHours() + seperator2 + date.getMinutes() +
		seperator2 + date.getSeconds();
	//将日期转换为数字格式，以备比较
	var nowTime = Date.parse(startTime);
	return nowTime;
}
//列表详情页面如请求不到数据，则跳转至暂无记录页
function noRecord(ele) {

	ele.html("<div class='norecord'><img src='../../images/noRecord.png'><p>暂无记录!</p></div>");
}



//通用弹框
function DoAlert(msg,lastHandler) {
	$("#AlertBlock").html(msg);
	$("#AlertBlock").fadeIn();
	//   $("body").css("opacity", 0.8);
	setTimeout(function () {
		$("#AlertBlock").fadeOut();
		$("body").css("opacity", 1);
			if (typeof (lastHandler) != undefined) {
				lastHandler();
			}
	}, 2000);
}

//获取Url参数
function GetRequest() {
	var url = location.search,
		theRequest = new Object;
	if (-1 != url.indexOf("?")) {
		var str = url.substr(1);
		strs = str.split("&");
		for (var i = 0; i < strs.length; i++)
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1])
	}
	return theRequest
}
$(document).ready(function () {
	setAppTitle();
});
